/*
 * Created by JFormDesigner on Mon Jul 01 15:04:59 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.ContactDao;
import scct.zhang.dao.ConversationDao;
import scct.zhang.dao.UserDao;
import scct.zhang.model.Contact;
import scct.zhang.model.User;
import scct.zhang.utils.Tools;

import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgConversationAdd extends JDialog {
    public DlgConversationAdd(Window owner) {
        super(owner);
        initComponents();
        initView();
    }
    private void initView() {
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        for (User user : allUser) {
            usernameBox.addItem(user.getUsername());
        }
        ContactDao contactDao = new ContactDao();
        List<Contact> allContact = contactDao.getAllContact();
        for (Contact contact : allContact) {
            contactnameBox.addItem(contact.getUsername());
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String username = (String) usernameBox.getSelectedItem();
        String contactname = (String) contactnameBox.getSelectedItem();
        String lasttime = Tools.getCreatetime();
        ConversationDao conversationDao = new ConversationDao();
        if (conversationDao.addConversation(username,contactname,lasttime)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }
        this.dispose();

    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
       this.dispose();
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yzx
        label1 = new JLabel();
        label2 = new JLabel();
        btnAdd = new JButton();
        btnCancle = new JButton();
        usernameBox = new JComboBox();
        contactnameBox = new JComboBox();

        //======== this ========
        setTitle("\u6dfb\u52a0\u4f1a\u8bdd");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(75, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u8054\u7cfb\u4eba");
        contentPane.add(label2);
        label2.setBounds(75, 85, 45, 17);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(55, 160), btnAdd.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(190, 160), btnCancle.getPreferredSize()));
        contentPane.add(usernameBox);
        usernameBox.setBounds(160, 30, 130, usernameBox.getPreferredSize().height);
        contentPane.add(contactnameBox);
        contactnameBox.setBounds(160, 70, 130, contactnameBox.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yzx
    private JLabel label1;
    private JLabel label2;
    private JButton btnAdd;
    private JButton btnCancle;
    private JComboBox usernameBox;
    private JComboBox contactnameBox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
